package principle.ocp;

/**
 * 开闭原则：对修改（使用方）关闭，对扩展（提供方）打开
 * @author shkstart
 * @create ${year}-${month}-${day}-${time}
 */
public class oc {
    public static void main(String[] args) {
        //存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}
//绘图，使用方
class GraphicEditor {
    public void drawShape(Shape s) {
        if (s.m_type == 1)
            drawRectangle(s);
        else if (s.m_type == 2)
            drawCircle(s);
        else if (s.m_type == 3)
            drawTriangle(s);
    }
    public void drawRectangle(Shape r) {
        System.out.println(" 绘制矩形 ");}
    public void drawCircle(Shape r) {
        System.out.println(" 绘制圆形 ");
    }
    public void drawTriangle(Shape r) {
        System.out.println(" 绘制三角形 ");
    }
}
//shape类，基类
class Shape {
    int m_type;
}
class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }}
class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }}
class Triangle extends Shape {
    Triangle() {
    super.m_type = 3;
}}